using System;
using NUnit.Framework;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;

namespace noug.tdd.tests {
    [TestFixture]
    public class When_adding_a_user {
        private AddNewUserController _controller;
        private IUserValidator _userValdidator;
        private User _user;
        private IUserRepository _userRepository;
        [SetUp]
        public void SetUp() {
            _userRepository = MockRepository.GenerateMock<IUserRepository>();
            _userValdidator = MockRepository.GenerateStub<IUserValidator>();
            _controller = new AddNewUserController(_userRepository, _userValdidator);
            _user = new User();
        }
        [Test]
        public void if_the_user_is_valid_save_to_repository() {
            _userRepository.ExpectSimilar(x => x.Save(null));
            _userValdidator.StubSimilar(x => x.IsValid(null)).Return(true);
            _controller.Create("name", "email", "address");
        }
        [Test]
        public void if_the_user_is_invalid_dont_save_to_repository() {
            _userRepository.DoNotExpectSimilar(x => x.Save(null));
            _userValdidator.StubSimilar(x => x.IsValid(_user)).Return(false);
            _controller.Create("name", "email", "address");
        }
        [Test]
        public void tries_to_store_a_user_with_the_same_fields_as_parameters() {
            _userRepository.Expect(x => x.Save(null)).Constraints(Is.Matching<User>(u=>
                u.Name == "name" && u.Email =="email" && u.Address=="address"));
            _userValdidator.StubSimilar(x => x.IsValid(null)).Return(true);
            _controller.Create("name", "email", "address");
        }
        [TearDown]
        public void TearDown() {
            _userRepository.VerifyAllExpectations();
        }
    }
}
